using System.ComponentModel;
using System.Runtime.CompilerServices;
using KinonekoSoftware.Foundation.Patterns;

#if AVALONIA
using ReactiveUI;
using ReactiveObject = ReactiveUI.ReactiveObject;

namespace KinonekoSoftware.Foundation
{
    partial class ManagedLifetimeObject: ReactiveObject, IDisposable, IDisposableCollector
    {
        
        //-------------------------------------------------------------
        //
        //          Dispose
        //
        //-------------------------------------------------------------

        #region Dispose

        protected virtual void ReleaseUnmanagedResources()
        {
        }

        protected virtual void ReleaseManagedResources()
        {
        }

        protected void Dispose(bool disposing)
        {
            ReleaseManagedResources();
            
            if (disposing)
            {
                ReleaseUnmanagedResources();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
        
        //-------------------------------------------------------------
        //
        //          RaiseUpdated / SetValue
        //
        //-------------------------------------------------------------

        /// <summary>
        /// 设置属性值，并提示数据更新
        /// </summary>
        /// <param name="source">原始的字段</param>
        /// <param name="value">值字段</param>
        /// <param name="name">属性名</param>
        /// <typeparam name="T">类型数据</typeparam>
        /// <returns>返回是否更新</returns>
        protected bool SetValue<T>(ref T source, T value, [CallerMemberName] string name = "")
        {
            if (string.IsNullOrEmpty(name)) return false;
            source = value;
            RaiseUpdated(name);
            return true;
        }

        /// <summary>
        /// 提示数据更新
        /// </summary>
        /// <param name="propertyName">属性名</param>
        protected void RaiseUpdated([CallerMemberName] string propertyName = "")
        {
            ((IReactiveObject)this).RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));

        }
    }
}

#endif